﻿@page "/customer"
@using System.Collections.ObjectModel
@inject DialogService DialogService
@inject CustomerService CustomerService

<RadzenText TextStyle="TextStyle.H4">客户管理</RadzenText>
<PageTitle>客户管理</PageTitle>
<RadzenStack Style="display:flex" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Start">
    <RadzenTextBox @bind-Value="@keyword" Placeholder="客户名称/手机号" Style="margin:10px 0px 10px 0px" />
    <RadzenButton Text="查询" Icon="refresh" Click=@(args => Search()) ButtonStyle="ButtonStyle.Primary" Style="margin:10px 0px 10px 0px" />
    <RadzenButton Text="新增" Icon="add_circle" Click=@(args => Add()) ButtonStyle="ButtonStyle.Primary" Style="margin:10px 0px 10px 0px" />
</RadzenStack>
<RadzenDataGrid EmptyText="暂无数据" TItem="Customer" Data="@customers" GridLines="Radzen.DataGridGridLines.Default" AllowPaging="true" AllowSorting="true">
    <Columns>
        <RadzenDataGridColumn Property="Id" Title="序号" Width="100px" TextAlign="TextAlign.Center" />
        <RadzenDataGridColumn Property="" Title="操作" Width="320px">
            <Template Context="data">
                <RadzenStack Gap="10" Style="margin-bottom: 10px" Orientation="Orientation.Horizontal">
                    <RadzenButton Text="收款" @onclick:stopPropagation Click="@(args=> ReceiveMoney(data))" ButtonStyle="ButtonStyle.Primary" />
                    <RadzenButton Text="修改" @onclick:stopPropagation Click="@(args=> Update(data))" ButtonStyle="ButtonStyle.Primary" />
                    <RadzenButton Text="删除" @onclick:stopPropagation Click="@(args=> Delete(data))" ButtonStyle="ButtonStyle.Danger" />
                    <RadzenButton Text="明细" @onclick:stopPropagation Click=@(args => ViewLogs(data)) ButtonStyle="ButtonStyle.Primary" />
                </RadzenStack>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="Name" Title="客户名称" />
        <RadzenDataGridColumn Property="Phone" Title="手机号" />
        <RadzenDataGridColumn Property="Address" Title="地址" />
        <RadzenDataGridColumn Property="Remark" Title="备注" />
    </Columns>
</RadzenDataGrid>

@code {
    string keyword = default!;
    ObservableCollection<Customer> customers = default!;

    async Task Search()
    {
        await Task.Run(() =>
        {
            var input = new BaseInput()
            {
                keyword = keyword
            };
            var list = CustomerService.SearchCustomer(input);
            customers = new ObservableCollection<Customer>(list);
        });
    }

    private async void ViewLogs(Customer customer)
    {
        var result = await DialogService.OpenAsync<CustomerPayLogDialog>($"账目明细",
               new Dictionary<string, object>() { { "PCustomer", customer } },
               new DialogOptions()
                   {
                       Resizable = true,
                       Draggable = true,
                       Width = "1000px",
                       Height = "612px",
                       Left = null,
                       Top = null
                   });
    }

    private async Task ReceiveMoney(Customer customer)
    {
        var log = new CustomerPayLog();
        var result = await DialogService.OpenAsync<CustomerReceiveMoneyAddDialog>($"收款",
               new Dictionary<string, object>() {
                   {"PCustomer", customer },
                   {"PLog", log },
                   },
               new DialogOptions()
                   {
                       Resizable = true,
                       Draggable = true,
                       Width = "700px",
                       Height = "512px",
                       Left = null,
                       Top = null
                   });
    }

    private async void Add()
    {
        var customer = new Customer();
        var result = await DialogService.OpenAsync<CustomerAddDialog>($"新增",
               new Dictionary<string, object>() {
                   {"PCustomer", customer },
                   {"PIsShow", true }
               },
               new DialogOptions()
                   {
                       Resizable = true,
                       Draggable = true,
                       Width = "700px",
                       Height = "512px",
                       Left = null,
                       Top = null
                   });
        if (result != null)
        {
            customers!.Add(result);
        }
    }

    private async void Update(Customer selectedItem)
    {
        Customer result = await DialogService.OpenAsync<CustomerAddDialog>($"修改",
               new Dictionary<string, object>() {
                   {"PCustomer", selectedItem },
                   {"PIsShow", false }
               },
               new DialogOptions()
                          {
                              Resizable = true,
                              Draggable = true,
                              Width = "700px",
                              Height = "512px",
                              Left = null,
                              Top = null
                          });
        if (result != null)
        {            
            StateHasChanged();
        }
    }

    private async void Delete(Customer selectedItem)
    {
        var result = await DialogService.Confirm("确认要删除吗?", "提示", new ConfirmOptions()
            {
                OkButtonText = "确定",
                CancelButtonText = "取消"
            });

        if ((bool)result)
        {
            try
            {
                CustomerService.DeleteCustomer(selectedItem);
                customers!.Remove(selectedItem);
            }
            catch (Exception e)
            {
                await DialogService.Alert(e.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await Search();
    }

}

